package com.easycoding.part04;

import java.io.FileNotFoundException;

/**
 * Created by hww on 2019/2/6 6:41 PM.
 */
public class CustomClassLoader extends ClassLoader {

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        try {
            byte[] result = getClassFromCustomPath(name);
            if (null == result) {
                throw new FileNotFoundException();
            } else {
                return defineClass(name, result, 0, result.length);
            }
        } catch (Exception e) {
            throw new ClassNotFoundException(name);
        }
    }


    private byte[] getClassFromCustomPath(String name) {
        // 从自定义路径中加载指定类
        return new byte[1024];
    }

    public static void main(String [] args) {
        CustomClassLoader customClassLoader = new CustomClassLoader();
        try {
            Class<?> clazz = Class.forName("One", true, customClassLoader);
            Object obj = clazz.newInstance();
            System.out.println(obj.getClass().getClassLoader());
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
